package org.example.controller;

import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import org.example.request.CartItemRequest;
import org.example.service.CartService;
import org.example.util.JsonData;
import org.example.vo.CartItemVO;
import org.example.vo.CartVO;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@Api(tags = "购物车模块")
@RestController
@RequestMapping("/api/cart/v1")
public class CartController {

    @Autowired
    private CartService cartService;

    @ApiOperation("加入到购物车")
    @PostMapping("add")
    public JsonData addToCart(@ApiParam("购物项") @RequestBody CartItemRequest cartItemRequest) {
        cartService.addToCart(cartItemRequest);
        return JsonData.buildSuccess();
    }

    @ApiOperation("清空购物车")
    @DeleteMapping("clear")
    public JsonData clearMyCart() {
        cartService.clearMyCart();
        return JsonData.buildSuccess();
    }

    @ApiOperation("查看我的购物车")
    @GetMapping("my_cart")
    public JsonData findMyCart() {
        CartVO cartVO = cartService.findMyCart();
        return JsonData.buildSuccess(cartVO);
    }

    @ApiOperation("根据商品id删除购物车")
    @DeleteMapping("/delete/{product_id}")
    public JsonData deleteCartById(@PathVariable(value = "product_id") Long productId) {
        cartService.deleteCartById(productId);
        return JsonData.buildSuccess();
    }

    /**
     * 更新购物车
     */
    @ApiOperation("更新购物车")
    @PostMapping("update")
    public JsonData updateCart(@ApiParam(value = "购物项", required = true)
                               @RequestBody CartItemRequest cartItemRequest) {
        cartService.updateCart(cartItemRequest);
        return JsonData.buildSuccess();
    }

    @ApiOperation("根据一些商品id查出购物项")
    @PostMapping("confirm_order_item")
    JsonData confirmOrderItem(@ApiParam("商品id列表") @RequestBody List<Long> productIds) {
        List<CartItemVO> cartItemVOList = cartService.confirmOrderItem(productIds);
        return JsonData.buildSuccess(cartItemVOList);
    }

}
